﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ANOWORD = Microsoft.Office.Interop.Word;
namespace HJ_SQL_ToTable
{
    public class MSWordTableCopyer
    {
        public int FromTableIndex;
        public int FromTableRowIndex;
        public int FromTableColumnIndex;

        public int ToTableStartIndex;
        public int ToTableRowIndex;
        public int ToTableColumnIndex;
        public int ToTableCount;

        public bool Copy(ANOWORD.Document docu)
        {
            bool bRet = false;
            ANOWORD.Tables tables;
            ANOWORD.Table fromTable;
            ANOWORD.Table toTable;
            string tmpString = string.Empty;
            try
            {
                tables = docu.Tables;
                do
                {
                    if (tables.Count < (ToTableStartIndex + ToTableCount - 1))
                    {
                        break;
                    }

                    fromTable = tables[FromTableIndex];
                    for (int i = 0; i < ToTableCount; i++) {
                        toTable = tables[ToTableStartIndex + i];

                        //toTable.Cell(ToTableRowIndex, ToTableColumnIndex).Range.Text = "";
                        
                        tmpString = fromTable.Cell(FromTableRowIndex + i, FromTableColumnIndex).Range.Text;
                        tmpString = tmpString.Replace("\r\a", "");
                        //tmpString = tmpString.Replace("\a", "");
                        //tmpString = tmpString.Substring(0, tmpString.Length - 1);
                        //if ( tmpString.Substring(tmpString.Length - 1, 1))
                        toTable.Cell(ToTableRowIndex, ToTableColumnIndex).Range.Text = tmpString;
                        //toTable.Cell(ToTableRowIndex, ToTableColumnIndex)
                        //toTable.Cell(ToTableRowIndex, ToTableColumnIndex).Range.Text = toTable.Cell(ToTableRowIndex, ToTableColumnIndex).Range.Text.Replace("\n", "");
                    }

                    bRet = true;

                } while (false);
            }
            catch (Exception ex)
            {

            }

            return bRet;
        }
    }
}
